package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Account;
@Entity
@Table(name="userlog")
public class Userlog extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	@ManyToOne
	public Account account; //用户
	
	public String action; //行为
	@JsonSerialize(using=JsonDatetimeFormatter.class)
	public Date updatetime; //发生时间
	
	public String model; //表格
	
	public Integer rowid; //记录
	
	public String field; //字段
	public static Model.Finder<Integer, Userlog> find = new Model.Finder<Integer, Userlog>(Integer.class, Userlog.class);

	public static Page<Userlog> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Userlog> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setAccount(Account d) {
		account= d;
	}
	public Account getAccount() {
		return account;
	}
	public void setAction(String d) {
		action= d;
	}
	public String getAction() {
		return action;
	}
	public void setUpdatetime(Date d) {
		updatetime= d;
	}
	public Date getUpdatetime() {
		return updatetime;
	}
	public void setModel(String d) {
		model= d;
	}
	public String getModel() {
		return model;
	}
	public void setRowid(Integer d) {
		rowid= d;
	}
	public Integer getRowid() {
		return rowid;
	}
	public void setField(String d) {
		field= d;
	}
	public String getField() {
		return field;
	}
	public static Userlog findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Userlog v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
}
